/* Includes ------------------------------------------------------------------*/
#include "global.h"
#include "ThermostatTask.h"
#include "var.h"
#include "KeyScan.h"
#include "comp.h"
#include "string.h"
#include "display.h"
#include "TimeEvent.h"
#include "nv_flash.h"
/*********************************************************************
 * MACROS
 */
/*********************************************************************
 * TYPEDEFS
 */
/*********************************************************************
 * CONSTANTS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */

/*********************************************************************
 * EXTERNAL VARIABLES
 */

/*********************************************************************
 * EXTERNAL FUNCTIONS
 */
/*********************************************************************
 * @fn    void St_ParasRangeUpate_Evt(void)
 *
 * @brief  参数内容+
 *
 * @param   none
 *
 * @return  none
 */
void St_ParasRangeUpate_Evt(uint8_t mode)
{
    st_ParaRange[0].min = cfgPara[mode].StMin;
    st_ParaRange[0].max = cfgPara[mode].StMax;
    st_ParaRange[1].max = cfgPara[mode].St;
    st_ParaRange[2].min = cfgPara[mode].St;
}

/*********************************************************************
 * @fn    void Page_cfgData_Increase_Evt(void)
 *
 * @brief  参数内容+
 *
 * @param   none
 *
 * @return  none
 */
void Page_cfgData_Increase_Evt(void)
{
    if (m_PageInfor.m_CfgPg >= 1 && m_PageInfor.m_CfgPg <= 3)
    {
        st_ParaRange[0].min = DuplicatePara.StMin;
        st_ParaRange[0].max = DuplicatePara.StMax;
        st_ParaRange[1].max = DuplicatePara.St;
        st_ParaRange[2].min = DuplicatePara.St;

        if (duplicate_sptr[m_PageInfor.m_CfgPg] < st_ParaRange[m_PageInfor.m_CfgPg - 1].max) //数据范围内操作
            duplicate_sptr[m_PageInfor.m_CfgPg]++;
        DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], st_ParaRange[m_PageInfor.m_CfgPg - 1].dp);
    }
    else
    {
        if (duplicate_sptr[m_PageInfor.m_CfgPg] < CfgParaRange[m_PageInfor.m_CfgPg].max) //数据范围内操作
            duplicate_sptr[m_PageInfor.m_CfgPg]++;
        DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);
    }

    // if (duplicate_sptr[m_PageInfor.m_CfgPg] < CfgParaRange[m_PageInfor.m_CfgPg].max) //数据范围内操作
    //     duplicate_sptr[m_PageInfor.m_CfgPg]++;
    // DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);
}

/*********************************************************************
 * @fn    void Page_cfgData_Decrease_Evt(void)
 *
 * @brief  参数内容-
 *
 * @param   none
 *
 * @return  none
 */
void Page_cfgData_Decrease_Evt(void)
{
    if (m_PageInfor.m_CfgPg >= 1 && m_PageInfor.m_CfgPg <= 3)
    {
        st_ParaRange[0].min = DuplicatePara.StMin;
        st_ParaRange[0].max = DuplicatePara.StMax;
        st_ParaRange[1].max = DuplicatePara.St;
        st_ParaRange[2].min = DuplicatePara.St;

        if (duplicate_sptr[m_PageInfor.m_CfgPg] > st_ParaRange[m_PageInfor.m_CfgPg - 1].min) //数据范围内操作
            duplicate_sptr[m_PageInfor.m_CfgPg]--;
        DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], st_ParaRange[m_PageInfor.m_CfgPg - 1].dp);
    }
    else
    {
        if (duplicate_sptr[m_PageInfor.m_CfgPg] > CfgParaRange[m_PageInfor.m_CfgPg].min) //数据范围内操作
            duplicate_sptr[m_PageInfor.m_CfgPg]--;
        DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);
    }

    // if (duplicate_sptr[m_PageInfor.m_CfgPg] > CfgParaRange[m_PageInfor.m_CfgPg].min) //数据范围内操作
    //     duplicate_sptr[m_PageInfor.m_CfgPg]--;
    // DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);
}

/*********************************************************************
 * @fn    void Page_cfgData_Increase_Evt(void)
 *
 * @brief  参数内容+
 *
 * @param   none
 *
 * @return  none
 */
void Page_cfgData_Increase2_Evt(void)
{
    if (m_PageInfor.m_CfgPg >= 1 && m_PageInfor.m_CfgPg <= 3)
    {
        st_ParaRange[0].min = DuplicatePara.StMin;
        st_ParaRange[0].max = DuplicatePara.StMax;
        st_ParaRange[1].max = DuplicatePara.St;
        st_ParaRange[2].min = DuplicatePara.St;

        duplicate_sptr[m_PageInfor.m_CfgPg]++;
        if (duplicate_sptr[m_PageInfor.m_CfgPg] > st_ParaRange[m_PageInfor.m_CfgPg - 1].max)
            duplicate_sptr[m_PageInfor.m_CfgPg] = st_ParaRange[m_PageInfor.m_CfgPg - 1].min;
        DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], st_ParaRange[m_PageInfor.m_CfgPg - 1].dp);
    }
    else
    {
        duplicate_sptr[m_PageInfor.m_CfgPg]++;
        if (duplicate_sptr[m_PageInfor.m_CfgPg] > CfgParaRange[m_PageInfor.m_CfgPg].max) //数据范围内操作
            duplicate_sptr[m_PageInfor.m_CfgPg] = CfgParaRange[m_PageInfor.m_CfgPg].min;
        DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);
    }
}

/*********************************************************************
 * @fn     void EEpromTask(void )
 *
 * @brief  内部flash存储处理
 *
 * @param   none
 *
 * @return  none
 */
void BackHomePage_Evt(void)
{
    Flag2.OneBit.Rt0Set = 0;
    Flag2.OneBit.PwRight = 0; //密码状态归0
    Flag3.OneBit.defrosteTempShow = FALSE;

    Flag3.OneBit.pressCount = 0;
    Flag3.OneBit.TestMode = 0; //退出测试模式

    memset((uint8_t *)&DuplicatePara.password, 0x00, sizeof(cfgPara_t)); //清除备用数据
    St_ParasRangeUpate_Evt(CoolingMode);

    m_PageInfor.m_Type = 0x00; //直接回到主页面
    m_PageInfor.m_CfgPg = 0x00;
    displayState = 0;
    Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 100, ENABLE, 0, DisplayRefurbish);
}

/*********************************************************************
 * @fn     void EEpromTask(void )
 *
 * @brief  内部flash存储处理
 *
 * @param   none
 *
 * @return  none
 */
void ExitCfgPageAndSavePara_Evt(void)
{
    timer_10msDB[CfgParaTimeout_TaskId].Flag = 0;
    timer_10msDB[CfgParaTimeout_TaskId].Enable = Disable; // ENABLE;
    timer_10msDB[CfgParaTimeout_TaskId].Time = 0;         // 200U;//500ms

    Flag2.OneBit.Lock = 1;
    BackHomePage_Evt();
}
/*********************************************************************
 * @fn     void EEpromTask(void )
 *
 * @brief  内部flash存储处理
 *
 * @param   none
 *
 * @return  none
 */
void EEpromSaveEvt(void)
{
    if (Flag1.OneBit.WriteEEP == 1)
    {
        Flag1.OneBit.WriteEEP = 0;
        if (Flag3.OneBit.eepThcfg == 1) //温控器参数配置
        {
            Flag3.OneBit.eepThcfg = 0;

            UserNV_EEPROMInit(COOLINGPARA_ADDR, 1, &CoolingMode, NV_EEPROM_Wirte);
            UserNV_EEPROMInit(CFGPARA_ADDR, sizeof(cfgPara_t)*2, (uint8_t *)&cfgPara[0].password, NV_EEPROM_Wirte); //配置控制器参数
            St_ParasRangeUpate_Evt(CoolingMode);
        }
    }
}


/*********************************************************************
 * @fn    void St_ParasRangeUpate_Evt(void)
 *
 * @brief  参数内容+
 *
 * @param   none
 *
 * @return  none
 */
void Test_Run_Evt(void)
{
    if (Flag2.OneBit.Blink)
    {
        Reg_CompressorState(ON);
        Reg_HeatState(OFF);
        Reg_FanState(ON);

        disDotStatus.OneBit.Colds = 1;
        disDotStatus.OneBit.Freeze = 0;
        disDotStatus.OneBit.Lock = 1;
        disDotStatus.OneBit.QuickFrozening = 0;
        if (ErrorState.OneBit.Ntc0Error == 0)
        {
            DisplayTempValue(TempInside);
        }
        else
        {
            DisplayDatasSymbol(Code_E, Num_0);
        }
    }
    else
    {

        Reg_CompressorState(OFF);
        Reg_HeatState(ON);
        Reg_FanState(OFF);

        disDotStatus.OneBit.Colds = 0;
        disDotStatus.OneBit.Freeze = 1;
        disDotStatus.OneBit.Lock = 0;
        disDotStatus.OneBit.QuickFrozening = 1;

        if (ErrorState.OneBit.Ntc1Error == 0)
        {
            DisplayTempValue(DefrostTemp);
        }
        else
        {
            DisplayDatasSymbol(Code_E, Num_1);
        }
    }
    DisplayDotSymbol(disDotStatus);
    Flag2.OneBit.Blink = ~Flag2.OneBit.Blink;
}

/*********************************************************************
 * @fn      void CfgParaTimeout_Evt( void )
 *
 * @brief   配置过程中超时事件
 *
 * @param   none
 *
 * @return  none
 */
// void CfgParaTimeout_Evt(void)
// {
// }